package com.mspbots.sync.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * com.mspbots.sync.config.RabbitConfigurer
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/7/29
 */
@Configuration
@RequiredArgsConstructor
public class RabbitConfiguration {

    private final AmqpAdmin amqpAdmin;

    @Bean
    public void declareCallbackExchange() {

        Exchange exchange = ExchangeBuilder.fanoutExchange("msp.wise.callback.tickets").build();
        this.amqpAdmin.declareExchange(exchange);
        Queue queue = QueueBuilder.durable("core.tickets").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("wise-tickets-core").noargs());


        exchange = ExchangeBuilder.fanoutExchange("msp.wise.callback.schedules").build();
        this.amqpAdmin.declareExchange(exchange);
        queue = QueueBuilder.durable("core.schedules").build();
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("wise-schedules-core").noargs());

    }

    @Bean
    public void declareFanoutExchange() {

        Exchange wiseAi = ExchangeBuilder.fanoutExchange("msp.wise.entries").build();
        this.amqpAdmin.declareExchange(wiseAi);

        Queue queue = new Queue("core.entries");
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(wiseAi).with("wise-entries-core").noargs());

        //msp.wise.sheet.times
        Exchange sheetTimesExchange = ExchangeBuilder.fanoutExchange("msp.wise.sheet.times").build();
        this.amqpAdmin.declareExchange(sheetTimesExchange);

        Queue queueSheetTimes = new Queue("core.sheet.times");
        this.amqpAdmin.declareQueue(queueSheetTimes);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueSheetTimes)
                .to(sheetTimesExchange).with("wise-sheet.times-core").noargs());

    }

    @Bean
    public void declareWiseUser() {
        Exchange exchange = ExchangeBuilder.directExchange("msp.wise.user").build();
        this.amqpAdmin.declareExchange(exchange);

        Queue queue = new Queue("wise.users");
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("wise-users-callback").noargs());

        exchange = ExchangeBuilder.directExchange("msp.sheet.user").build();
        this.amqpAdmin.declareExchange(exchange);

        queue = new Queue("sheet.users");
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("sheet-users-callback").noargs());

        queue = new Queue("sheet.users.mapping");
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(exchange).with("sheet-users-callback").noargs());
    }

    @Bean
    public void declareWiseAi() {
        Exchange wiseAi = ExchangeBuilder.directExchange("msp.wise.ai").build();
        this.amqpAdmin.declareExchange(wiseAi);

        Queue queueScheduleStatuses = new Queue("wise.subdata");
        this.amqpAdmin.declareQueue(queueScheduleStatuses);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueScheduleStatuses)
                .to(wiseAi).with("wise-subdata-callback").noargs());

        Queue queueBoardAssociations = new Queue("wise.board.associations");
        this.amqpAdmin.declareQueue(queueBoardAssociations);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueBoardAssociations)
                .to(wiseAi).with("wise-board-associations-callback").noargs());

        Queue queueCompanies = new Queue("wise.companies");
        this.amqpAdmin.declareQueue(queueCompanies);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueCompanies)
                .to(wiseAi).with("wise-companies-callback").noargs());

        Queue queueSlas = new Queue("wise.slas");
        this.amqpAdmin.declareQueue(queueSlas);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueSlas)
                .to(wiseAi).with("wise-slas-callback").noargs());

        Queue queueSkills = new Queue("wise.skills");
        this.amqpAdmin.declareQueue(queueSkills);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueSkills)
                .to(wiseAi).with("wise-skills-callback").noargs());

        Queue queueBoard = new Queue("wise.board");
        this.amqpAdmin.declareQueue(queueBoard);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueBoard)
                .to(wiseAi).with("wise-board-callback").noargs());

        Queue queueProjectPhase = new Queue("wise.project.phase");
        this.amqpAdmin.declareQueue(queueProjectPhase);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueProjectPhase)
                .to(wiseAi).with("wise-phase-callback").noargs());

        Queue queuePriority = new Queue("wise.priority");
        this.amqpAdmin.declareQueue(queuePriority);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queuePriority)
                .to(wiseAi).with("wise-priority-callback").noargs());

        Queue queueAgreements = new Queue("wise.agreements");
        this.amqpAdmin.declareQueue(queueAgreements);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueAgreements)
                .to(wiseAi).with("wise-agreements-callback").noargs());

        Queue queueContacts = new Queue("wise.contacts");
        this.amqpAdmin.declareQueue(queueContacts);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queueContacts)
                .to(wiseAi).with("wise-contacts-callback").noargs());

        Queue queue = new Queue("exception.log");
        this.amqpAdmin.declareQueue(queue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(queue)
                .to(wiseAi).with("wise-exception.log").noargs());

        Queue callbackQueue = new Queue("callback.history");
        this.amqpAdmin.declareQueue(callbackQueue);
        this.amqpAdmin.declareBinding(BindingBuilder.bind(callbackQueue)
                .to(wiseAi).with("wise-callback.history").noargs());
    }

    @Bean
    public MessageConverter rabbitMessageConverter(ObjectMapper objectMapper) {
        return new Jackson2JsonMessageConverter(objectMapper);
    }
}
